// mmo_game_server/player/authentication.go
package player

import (
    "crypto/sha256"
    "encoding/hex"
    "errors"
    "your_project/data"
)

// HashPassword 对密码进行哈希处理
func HashPassword(password string) string {
    hash := sha256.Sum256([]byte(password))
    return hex.EncodeToString(hash[:])
}

// VerifyPassword 验证密码
func VerifyPassword(hashedPassword, password string) bool {
    return hashedPassword == HashPassword(password)
}

// AuthenticatePlayer 验证玩家身份
func AuthenticatePlayer(username, password string) (int64, error) {
    db := data.GetDB()
    var accountID int64
    var hashedPassword string
    err := db.QueryRow("SELECT id, password FROM accounts WHERE username = $1", username).Scan(&accountID, &hashedPassword)
    if err != nil {
        return 0, errors.New("invalid credentials")
    }
    if!VerifyPassword(hashedPassword, password) {
        return 0, errors.New("invalid credentials")
    }
    return accountID, nil
}